package com.shop2cn.wxcomponent.testcase;

import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.wxcomponent.parameter.AuthorizationCallBackBean;
import com.shop2cn.wxcomponent.service.AuthorizationCallBackCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.WxComponentService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.Mchapp;
import com.ymttest.database.model.Platformconfig;
import com.ymttest.database.sqlwapper.godmch.DefGodMchWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
/**
*授权变更回调
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_AuthorizationCallBack{
	private static AuthorizationCallBackBean authorizationcallbackBean;
	private static AuthorizationCallBackCall authorizationcallbackCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("授权变更回调");
	}

	@Before
	public void caseUp() {
		authorizationcallbackBean=new AuthorizationCallBackBean();
		authorizationcallbackCall=new AuthorizationCallBackCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(authorizationcallbackCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore 
	//这个场景会更新平台ticket 
	public void Tc_AuthorizationCallBack_001() {
		Logger.start(true,"授权变更回调-component_verify_ticket更新平台ticket");
		try {
			String timestamp=String.valueOf(YMTDateUtil.getDate().getTime()/1000);
			Platformconfig platf = new DefGodMchWapper().selectPlatformconfigByExample().get(0);
			authorizationcallbackBean.setAppId(platf.getPlatformAppId());
			authorizationcallbackBean.setCreateTime(Long.valueOf(timestamp));
			authorizationcallbackBean.setInfoType("component_verify_ticket");
			authorizationcallbackBean.setComponentVerifyTicket("Ticket");
			Map sendinfo=WxComponentService.getAuthorizationcallbackSend(authorizationcallbackBean);
//			Logger.debug(sendinfo);
			String signature=sendinfo.get("signature").toString();
			String mingwen=sendinfo.get("mingwen").toString();
			String nonce=sendinfo.get("nonce").toString();
			String opurl2="api/authorizeCallBackNotice?timestamp="+timestamp+"&nonce="+nonce+"&encrypt_type=aes&msg_signature="+signature;
			authorizationcallbackCall=new AuthorizationCallBackCall(opurl2);
			authorizationcallbackCall.setData(mingwen);
			authorizationcallbackCall.callService();	
			Logger.verifyEquals("ok", authorizationcallbackCall.getReturnData(), "验证返回");
			String result2 =WxComponentService.decryptMsg(sendinfo.get("encrypt").toString(), platf.getPlatformAppId());
			System.out.println("解密后明文: " + result2);
			String dbVerifyTicket=new DefGodMchWapper().selectPlatformconfigByExample().get(0).getVerifyTicket();
			Logger.verifyEquals(authorizationcallbackBean.getComponentVerifyTicket(), dbVerifyTicket, "验证ComponentVerifyTicket");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_AuthorizationCallBack_002() {
		Logger.start(true,"授权变更回调-updateauthorized更新授权成功");
		try {
			Integer mchid=2003;
			WxComponentService.deleteMchinfo(mchid);
			String appid = WxComponentService.ConfirmAuthorization(mchid);
			WxComponentService.AuthorizationCallBack(appid, "authorized");
			String timestamp=String.valueOf(YMTDateUtil.getDate().getTime()/1000);
			Platformconfig platf = new DefGodMchWapper().selectPlatformconfigByExample().get(0);
			authorizationcallbackBean.setAppId(platf.getPlatformAppId());
			authorizationcallbackBean.setCreateTime(Long.valueOf(timestamp));
			authorizationcallbackBean.setInfoType("updateauthorized");
			authorizationcallbackBean.setAuthorizerAppid(appid);
			Map sendinfo=WxComponentService.getAuthorizationcallbackSend(authorizationcallbackBean);
//			Logger.debug(sendinfo);
			String signature=sendinfo.get("signature").toString();
			String mingwen=sendinfo.get("mingwen").toString();
			String nonce=sendinfo.get("nonce").toString();
			String opurl2="api/authorizeCallBackNotice?timestamp="+timestamp+"&nonce="+nonce+"&encrypt_type=aes&msg_signature="+signature;
			authorizationcallbackCall=new AuthorizationCallBackCall(opurl2);
			authorizationcallbackCall.setData(mingwen);
			authorizationcallbackCall.callService();	
			Logger.verifyEquals("ok", authorizationcallbackCall.getReturnData(), "验证返回");
			Mchapp mchapp = new DefGodMchWapper().selectMchappByExample(mchid).get(0);
			Logger.verifyEquals(mchapp.getAppStatus(), 2, "验证AppStatus");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_AuthorizationCallBack_003() {
		Logger.start(true,"授权变更回调-unauthorized取消授权成功");
		try {
			
			Integer mchid=2003;
			WxComponentService.deleteMchinfo(mchid);
			String appid = WxComponentService.ConfirmAuthorization(mchid);
			WxComponentService.AuthorizationCallBack(appid, "authorized");
			String timestamp=String.valueOf(YMTDateUtil.getDate().getTime()/1000);
			Platformconfig platf = new DefGodMchWapper().selectPlatformconfigByExample().get(0);
			authorizationcallbackBean.setAppId(platf.getPlatformAppId());
			authorizationcallbackBean.setCreateTime(Long.valueOf(timestamp));
			authorizationcallbackBean.setInfoType("unauthorized");
			authorizationcallbackBean.setAuthorizerAppid(appid);
			Map sendinfo=WxComponentService.getAuthorizationcallbackSend(authorizationcallbackBean);
//			Logger.debug(sendinfo);
			String signature=sendinfo.get("signature").toString();
			String mingwen=sendinfo.get("mingwen").toString();
			String nonce=sendinfo.get("nonce").toString();
			String opurl2="api/authorizeCallBackNotice?timestamp="+timestamp+"&nonce="+nonce+"&encrypt_type=aes&msg_signature="+signature;
			authorizationcallbackCall=new AuthorizationCallBackCall(opurl2);
			authorizationcallbackCall.setData(mingwen);
			authorizationcallbackCall.callService();	
			Logger.verifyEquals("ok", authorizationcallbackCall.getReturnData(), "验证返回");
			int i = new DefGodMchWapper().selectMchappByExample(mchid).size();
			Logger.verifyEquals(i, 0, "验证AppStatus");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_AuthorizationCallBack_004() {
		Logger.start(true,"授权变更回调-authorized授权成功");
		try {
			Integer mchid=2004;
			WxComponentService.deleteMchinfo(mchid);
			String appid = WxComponentService.ConfirmAuthorization(mchid);
			String timestamp=String.valueOf(YMTDateUtil.getDate().getTime()/1000);
			Platformconfig platf = new DefGodMchWapper().selectPlatformconfigByExample().get(0);
			authorizationcallbackBean.setAppId(platf.getPlatformAppId());
			authorizationcallbackBean.setCreateTime(Long.valueOf(timestamp));
			authorizationcallbackBean.setInfoType("authorized");
			authorizationcallbackBean.setAuthorizerAppid(appid);
			Map sendinfo=WxComponentService.getAuthorizationcallbackSend(authorizationcallbackBean);
//			Logger.debug(sendinfo);
			
			String signature=sendinfo.get("signature").toString();
			String mingwen=sendinfo.get("mingwen").toString();
			String nonce=sendinfo.get("nonce").toString();
			String opurl2="api/authorizeCallBackNotice?timestamp="+timestamp+"&nonce="+nonce+"&encrypt_type=aes&msg_signature="+signature;
			authorizationcallbackCall=new AuthorizationCallBackCall(opurl2);
			authorizationcallbackCall.setData(mingwen);
			authorizationcallbackCall.callService();	
			Logger.verifyEquals("ok", authorizationcallbackCall.getReturnData(), "验证返回");
			Mchapp mchapp = new DefGodMchWapper().selectMchappByExample(mchid).get(0);
			Logger.verifyEquals(mchapp.getAppStatus(), 1, "验证AppStatus");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_AuthorizationCallBack_005() {
		Logger.start(true,"取消授权成功");
		try {
			Integer mchid = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
            String timestamp=String.valueOf(YMTDateUtil.getDate().getTime()/1000);
            WxComponentService.deleteMchinfo(mchid);
			String appid = WxComponentService.ConfirmAuthorization(mchid);
			WxComponentService.AuthorizationCallBack(appid, "authorized");
			Platformconfig platf = new DefGodMchWapper().selectPlatformconfigByExample().get(0);
			authorizationcallbackBean.setAppId(platf.getPlatformAppId());
			authorizationcallbackBean.setCreateTime(Long.valueOf(timestamp));
			authorizationcallbackBean.setInfoType("unauthorized");
			authorizationcallbackBean.setAuthorizerAppid(appid);
			Map sendinfo=WxComponentService.getAuthorizationcallbackSend(authorizationcallbackBean);
//			Logger.debug(sendinfo);
			String signature=sendinfo.get("signature").toString();
			String mingwen=sendinfo.get("mingwen").toString();
			String nonce=sendinfo.get("nonce").toString();
			String opurl2="api/authorizeCallBackNotice?timestamp="+timestamp+"&nonce="+nonce+"&encrypt_type=aes&msg_signature="+signature;
			authorizationcallbackCall=new AuthorizationCallBackCall(opurl2);
			authorizationcallbackCall.setData(mingwen);
			authorizationcallbackCall.callService();	
			Logger.verifyEquals("ok", authorizationcallbackCall.getReturnData(), "验证返回");
			int size = new DefGodMchWapper().selectMchappByExample(mchid).size();
			Logger.verifyEquals(0,size, "验证AppStatus");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_AuthorizationCallBack_006() {
		Logger.start(true,"绑定，授权，解绑，授权--成功");
		try {
			Integer mchid = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			WxComponentService.deleteMchinfo(mchid);
			
			String appid = WxComponentService.ConfirmAuthorization(mchid);
			String timestamp=String.valueOf(YMTDateUtil.getDate().getTime()/1000);
			Platformconfig platf = new DefGodMchWapper().selectPlatformconfigByExample().get(0);
			authorizationcallbackBean.setAppId(platf.getPlatformAppId());
			authorizationcallbackBean.setCreateTime(Long.valueOf(timestamp));
			authorizationcallbackBean.setInfoType("authorized");
			authorizationcallbackBean.setAuthorizerAppid(appid);
			Map sendinfo=WxComponentService.getAuthorizationcallbackSend(authorizationcallbackBean);
//			Logger.debug(sendinfo);
			String signature=sendinfo.get("signature").toString();
			String mingwen=sendinfo.get("mingwen").toString();
			String nonce=sendinfo.get("nonce").toString();
			String opurl2="api/authorizeCallBackNotice?timestamp="+timestamp+"&nonce="+nonce+"&encrypt_type=aes&msg_signature="+signature;
			authorizationcallbackCall=new AuthorizationCallBackCall(opurl2);
			authorizationcallbackCall.setData(mingwen);
			authorizationcallbackCall.callService();
			DGUserCallService.unWxBind(mchid);
			authorizationcallbackCall.callService();
			Logger.verifyEquals("ok", authorizationcallbackCall.getReturnData(), "验证返回");
			Mchapp mchapp = new DefGodMchWapper().selectMchappByExample(mchid).get(0);
			Logger.verifyEquals(mchapp.getAppStatus(), 1, "验证AppStatus");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
}